import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../utils/request.dart';
import '../utils/adapt.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

class VideoList extends StatefulWidget {
  VideoList({Key? key}) : super(key: key);

  @override
  _VideoListState createState() => _VideoListState();
}

class _VideoListState extends State<VideoList>
    with AutomaticKeepAliveClientMixin {
  late ScrollController _scrollController = ScrollController();
  int _page = 1;
  List _list = [];
  bool _all = false;
  void _getDate(page) async {
    EasyLoading.show(status: 'loading...');
    var res = await getHttp('/top/mv?offset=$page&limit=20');
    EasyLoading.dismiss();
    if (res['data'].length < 20) {
      _all = true;
    } else {
      _all = false;
    }
    this.setState(() {
      _list.addAll(res['data']);
      _all = _all;
    });
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    this._getDate(this._page);
    _scrollController.addListener(() {
      if (_scrollController.position.pixels >
          _scrollController.position.maxScrollExtent - 40) {
        if (this._all == false) {
          this.setState(() {
            _page += 1;
          });
          _getDate(this._page);
        }
      }
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();

    _scrollController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          '视频列表',
          style: TextStyle(fontSize: Adapt.px(30)),
        ),
      ),
      body: ListView.separated(
        itemCount: _list.length,
        controller: _scrollController,
        separatorBuilder: (BuildContext context, int index) {
          return Divider(height: 1.0, color: Colors.red);
        },
        itemBuilder: (BuildContext context, int index) {
          return InkWell(
            onTap: () {
              var dataObj = _list[index];
              Navigator.pushNamed(context, "/videoplayer", arguments: {
                "vid": dataObj['id'],
                "cover": dataObj['cover'],
                "name": dataObj['name'],
                "desc": dataObj['desc'],
                "artistName": dataObj['artistName'],
              });
            },
            child: Container(
              height: Adapt.px(240),
              padding: EdgeInsets.symmetric(
                  vertical: Adapt.px(12), horizontal: Adapt.px(20)),
              child: Row(
                children: [
                  Container(
                    child: Image.network(
                      _list[index]['cover'] + '?param=300y180',
                      width: Adapt.px(400),
                    ),
                  ),
                  Container(
                    width: Adapt.px(300),
                    child: Column(
                      children: [
                        Text(
                          _list[index]['name'],
                          maxLines: 3,
                          style: TextStyle(
                            fontSize: Adapt.px(26),
                          ),
                        ),
                        Text(
                          _list[index]['artistName'],
                          style: TextStyle(
                            fontSize: Adapt.px(26),
                          ),
                        ),
                        Text(
                          _list[index]['mv']['publishTime'],
                          style: TextStyle(
                            fontSize: Adapt.px(26),
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          );
        },
      ),
    );
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
